package com.h.blog.controller;

import com.h.blog.pojo.request.UserLoginRequest;
import com.h.blog.pojo.response.UserInfoResponse;
import com.h.blog.pojo.response.UserLoginResponse;
import com.h.blog.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource(name = "userServiceImpl")
    private UserService userService;

    @RequestMapping("/login")
    // 因为是 JSON 类型的传入需要添加注解 @RequestBody 校验变量需要使用这个@Validated注解
    public UserLoginResponse login(@RequestBody @Validated UserLoginRequest userLoginRequest) {
      log.info("用户登陆 用户名: " + userLoginRequest.getUserName());
      return userService.checkPassword(userLoginRequest);
    }

    @RequestMapping("/getUserInfo")
    public UserInfoResponse getUserInfo(@NotNull(message = "userId不能为空") Integer userId) {
        log.info("获取登录的用户信息，userId: " + userId);
        return userService.getUserInfo(userId);
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfoResponse getAuthorInfo(@NotNull(message = "blogId不能为空") Integer blogId) {
        log.info("获取当前文章作者的用户信息，blogId: " + blogId);
        return userService.getAuthorInfo(blogId);
    }
}
